package com.woniuxy.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.food.infra.mapper.FoodTypeSpuMapper;
import com.woniuxy.food.infra.model.FoodType;
import com.woniuxy.food.infra.mapper.FoodTypeMapper;
import com.woniuxy.food.infra.model.FoodTypeSpu;
import com.woniuxy.food.service.FoodTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.food.service.servicedto.FoodTypeServiceDto;
import com.woniuxy.food.service.serviceparam.InsertFoodTypeServiceParam;
import com.woniuxy.food.service.serviceparam.SearchFoodTypeByConditionServiceParam;
import com.woniuxy.food.service.serviceparam.UpdateFoodTypeServiceParam;
import com.woniuxy.food.service.serviceparam.UpdateFoodTypeStateServiceParam;
import exception.MallException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜品类型表 服务实现类
 * </p>
 *
 * @author 刘德华
 * @since 2023-07-19
 */
@Service
public class FoodTypeServiceImpl extends ServiceImpl<FoodTypeMapper, FoodType> implements FoodTypeService {

    @Autowired
    private FoodTypeMapper foodTypeMapper;
    @Autowired
    private FoodTypeSpuMapper foodTypeSpuMapper;
    @Override
    @GlobalTransactional
    public void insertFoodType(InsertFoodTypeServiceParam param) {
        QueryWrapper<FoodType> foodTypeQueryWrapper = new QueryWrapper<>();
        foodTypeQueryWrapper.eq("food_type_name",param.getFoodTypeName()).eq("store_id",param.getStoreId());
        FoodType foodType = foodTypeMapper.selectOne(foodTypeQueryWrapper);
        if (foodType!=null) {
            throw new MallException("菜品类型名称不可以重复",CommonResultCodeEnum.INVALID_PARAM);
        }
        FoodType foodTypeNew = BeanUtil.toBean(param, FoodType.class);
        foodTypeNew.setFoodTypeState("FORBID");
        foodTypeMapper.insert(foodTypeNew);
    }

    @Override
    @GlobalTransactional
    public void updateFoodTypeStateByIds(UpdateFoodTypeStateServiceParam param) {
        List<Integer> ids = Arrays.asList(param.getIds());
        List<FoodType> foodTypes = new ArrayList<>();
        for (Integer id : ids) {
            QueryWrapper<FoodType> foodTypeQueryWrapper = new QueryWrapper<>();
            foodTypeQueryWrapper.eq("store_id",param.getStoreId()).eq("food_type_id",id);
            FoodType foodType = foodTypeMapper.selectOne(foodTypeQueryWrapper);
            if (foodType==null) {
                throw new MallException("菜品类型不存在", CommonResultCodeEnum.INVALID_PARAM);
            }
            foodTypes.add(foodType);
        }
        for (FoodType foodType : foodTypes) {
            if (foodType.getFoodTypeState().equals(param.getFoodTypeState())) {
                throw new MallException("有菜品类型已为该状态",CommonResultCodeEnum.INVALID_PARAM);
            }
        }
        for (FoodType foodType : foodTypes) {
            foodType.setFoodTypeState(param.getFoodTypeState());
            foodTypeMapper.updateById(foodType);
        }
    }

    @Override
    @GlobalTransactional
    public void updateFoodTypeById(UpdateFoodTypeServiceParam param) {
        QueryWrapper<FoodType> foodTypeQueryWrapper = new QueryWrapper<>();
        foodTypeQueryWrapper.eq("store_id",param.getStoreId()).eq("food_type_id",param.getFoodTypeId());
        FoodType foodType = foodTypeMapper.selectOne(foodTypeQueryWrapper);
        if (foodType==null) {
            throw new MallException("当前菜品类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        foodType.setFoodTypeName(param.getFoodTypeName());
        foodTypeMapper.updateById(foodType);
    }

    @Override
    @GlobalTransactional
    public void deleteFoodTypeByIds(Integer[] ids, Integer storeId) {
        List<Integer> idList = Arrays.asList(ids);
        List<FoodType> foodTypes = new ArrayList<>();
        for (Integer id : idList) {
            QueryWrapper<FoodType> foodTypeQueryWrapper = new QueryWrapper<>();
            foodTypeQueryWrapper.eq("store_id",storeId).eq("food_type_id",id);
            FoodType foodType = foodTypeMapper.selectOne(foodTypeQueryWrapper);
            if (foodType==null) {
                throw new MallException("菜品类型不存在", CommonResultCodeEnum.INVALID_PARAM);
            }
            QueryWrapper<FoodTypeSpu> foodTypeSpuQueryWrapper = new QueryWrapper<>();
            foodTypeSpuQueryWrapper.eq("food_type_id",id);
            List<FoodTypeSpu> foodTypeSpus = foodTypeSpuMapper.selectList(foodTypeSpuQueryWrapper);
            if (foodTypeSpus.size()!=0) {
                throw new MallException("菜品类型下还有菜品spu", CommonResultCodeEnum.INVALID_PARAM);
            }
            foodTypes.add(foodType);
        }

        for (FoodType foodType : foodTypes) {
            foodTypeMapper.deleteById(foodType);
        }
    }

    @Override
    public List<FoodTypeServiceDto> searchFoodTypeByCondition(SearchFoodTypeByConditionServiceParam param) {
        QueryWrapper<FoodType> foodTypeQueryWrapper = new QueryWrapper<>();
        foodTypeQueryWrapper.eq("store_id",param.getStoreId());
        if (param.getKeyword()!=null&&!"".equals(param.getKeyword())) {
            foodTypeQueryWrapper.like("food_type_name",param.getKeyword());
        }
        if (param.getFoodTypeState()!=null&&!"".equals(param.getFoodTypeState())) {
            foodTypeQueryWrapper.eq("food_type_state",param.getFoodTypeState());
        }
        List<FoodType> foodTypes = foodTypeMapper.selectList(foodTypeQueryWrapper);
        List<FoodTypeServiceDto> foodTypeServiceDtos = BeanUtil.copyToList(foodTypes, FoodTypeServiceDto.class);
        return foodTypeServiceDtos;
    }
}
